package com.atguigu.tingshu.album.mapper;

import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.query.album.AlbumInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumListVo;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.ServletContext;
import org.apache.ibatis.annotations.*;

@Mapper
public interface AlbumInfoMapper extends BaseMapper<AlbumInfo> {


    /**
     * 分页查询用户专辑列表
     * @param page 这个标注会通过mybatisPlus分页插件自动将分页条件追加到sql语句末尾；
     *             原理是拦截器，把limit（offset）后面的数据加到下一页
     * @param albumInfoQuery
     * @return
     */
    IPage<AlbumListVo> selectUserAlbumPage(Page<AlbumListVo> page,@Param("vo")AlbumInfoQuery albumInfoQuery);


    /**
     * 根据专辑id查询专辑统计数据
     * @param albumId
     * @return
     */
    @Select("select stat.album_id,\n" +
            "        max(if(stat.stat_type = '0401',stat.stat_num,0)) playStatNum,\n" +
            "        max(if(stat.stat_type = '0402',stat.stat_num,0)) subscribeStatNum,\n" +
            "        max(if(stat.stat_type = '0403',stat.stat_num,0)) buyStatNum,\n" +
            "        max(if(stat.stat_type = '0404',stat.stat_num,0)) commentStatNum\n" +
            "from album_stat stat\n" +
            "where stat.album_id = #{albumId} and stat.is_deleted = 0\n" +
            "group by stat.album_id")
    AlbumStatVo selectAlbumStat(@Param("albumId") Long albumId);

    /**
     * 更新专辑播放量，sql语句返回影响行数，所以方法返回值是integer
     * @param albumId
     * @param statType
     * @param count
     * @return
     */
    Integer updateStat(@Param("albumId") Long albumId,@Param("statType") String statType,@Param("count") Integer count);


    /**
     * 更新专辑统计信息
     * @param count
     * @param albumId
     * @param statPlay
     * @return
     */
    @Update("update album_stat set stat_num = stat_num + #{count} where album_id = #{albumId} and stat_type = #{statPlay} and is_deleted = 0")
    int updateAlbumStat(@Param("count") Integer count,@Param("albumId") Long albumId,@Param("statPlay") String statPlay);
}
